import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../config/api.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';
import 'package:toast/toast.dart';
class Loginpage extends StatefulWidget {
  Loginpage({Key key}) : super(key: key);

  @override
  _LoginpageState createState() => _LoginpageState();
}

class _LoginpageState extends State<Loginpage> {
   TextEditingController _user = TextEditingController();
    TextEditingController _password = TextEditingController();
    bool passwordVisible=false;
    //用户的信息
    Map user;
    @override
    void initState(){
      passwordVisible = false; 
    }
    void _login(v)async{
      getuserlogin(v).then((value) => {
        print(value),
        if(value.data['err']==0){
          addtoken(value.data['jwt']),
          _toastTow1(),
          Navigator.pushNamed(context, '/')
        }else{
          _toastTow()
        }
      });
    }
    //保存用户的token
  
//账号或密码错误
_toastTow()async{
    Toast.show("账号或密码错误", context,
        duration: Toast.LENGTH_SHORT, gravity: Toast.CENTER);
  }
  //登录成功正在跳转
  _toastTow1()async{
    Toast.show("登录成功正在跳转", context,
        duration: Toast.LENGTH_SHORT, gravity: Toast.CENTER);
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
       resizeToAvoidBottomInset:false,
      // appBar: AppBar(
      //   leading: InkWell(
      //     onTap: (){
      //       Navigator.pushNamed(context, '/register');
      //     },
      //     child: Center( child:Text('注册')),
      //   ),
      //   actions: <Widget>[
      //     IconButton(icon: Icon(Icons.close), onPressed: (){
      //       Navigator.pop(context);
      //     })
      //   ],
      //   title: Center( child: Text('登录'),),
      // ),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: (){
          print('我啊');
           FocusScope.of(context).requestFocus(FocusNode());
        },
        child: 
        Container(
          decoration: BoxDecoration(
            image: DecorationImage(image:NetworkImage("http://dbyxs.xyz:3005/uploads/c2a59c464936f30a34109531d89a9a8f",),fit:BoxFit.cover)
          ),
        padding: EdgeInsets.all(10),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          // crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Container(
              padding: EdgeInsets.only(top:40),
              height: ScreenUtil().setHeight(450),
              width: ScreenUtil().setWidth(750),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  InkWell( child: Text('注册',style: TextStyle(color: Colors.white,fontSize: 16,fontWeight: FontWeight.w600),),onTap: (){ Navigator.pushNamed(context, '/register');},),
                 // IconButton(icon: Icon(Icons.chevron_left,size: 36,), onPressed: (){ Navigator.pushNamed(context, '/register');}),
                  Text('你好 请登录',style: TextStyle(color: Colors.white,fontSize: 18),),
                InkWell(child:  Text('关闭',style: TextStyle(color:Colors.white),),onTap:(){Navigator.pop(context);})
                //  Container(
                //    padding: EdgeInsets.only(bottom:40),
                //    height: ScreenUtil().setHeight(50),
                //    child:  IconButton(icon: Icon(Icons.close), onPressed: (){Navigator.pop(context);}),
                //  )
                ],
              ),
            ),
            TextField(
               controller: _user,
               obscureText: false,
              decoration: InputDecoration(
                prefixIcon: IconButton(icon: Icon(Icons.account_box), onPressed: null),
                 disabledBorder: InputBorder.none,
                 // focusedBorder:   InputBorder.none,
                //  enabledBorder:  InputBorder.none,
                //border: OutlineInputBorder(
                  // borderSide: BorderSide( color: Colors.black26,),
                //),
                focusColor: Colors.black26,
                focusedBorder: OutlineInputBorder( //选中时外边框颜色
                          borderRadius: BorderRadius.circular(5.0),
                          borderSide: BorderSide( color: Colors.black26,),
                            ),
                 hintText: "手机号",
              ),
              
              onChanged: (v){
                print(v);
                _user=v as TextEditingController;
              },
            ),
            SizedBox(
              height: 10,
            ),
             TextField(
               controller: _password,
                obscureText: passwordVisible,
              decoration: InputDecoration(
                prefixIcon: IconButton(icon: Icon(Icons.next_week), onPressed: null),
                suffixIcon: IconButton(icon: Icon( passwordVisible ? Icons.visibility : Icons.visibility_off,
                color: Theme.of(context).primaryColorDark,), onPressed: (){
            setState(() {
          passwordVisible = !passwordVisible;
        });
        }),
                 disabledBorder: InputBorder.none,
                 // focusedBorder:   InputBorder.none,
                //  enabledBorder:  InputBorder.none,
                //border: OutlineInputBorder(
                  // borderSide: BorderSide( color: Colors.black26,),
                //),
                focusColor: Colors.black26,
                focusedBorder: OutlineInputBorder( //选中时外边框颜色
                          borderRadius: BorderRadius.circular(5.0),
                          borderSide: BorderSide( color: Colors.black26,),
                            ),
                 hintText: "密码",
              ),
              
              onChanged: (v){
              
              },
            ),
            SizedBox(
              height: 10,
            ),
            Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(15)
              ),
              padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
              width: ScreenUtil().setWidth(750),
              height: ScreenUtil().setHeight(80),
              child: RaisedButton(
                 shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(22.0))),
                color: Color(0xff25D1D1),
                onPressed: (){
                 // print('登录');
                 print(_user.text);
                 print(_password.text);
                 var l={"user":_user.text,"password":_password.text};
                 setState(() {
                   user=l;
                 });
                 _login(user);
                },child:Center(child: Text('登 录',style: TextStyle(fontSize:16,color:Colors.white,fontWeight: FontWeight.w600),),)),
            ),
            SizedBox( height: 5,),
            Container(
              child: InkWell(
                child: Center(child:Text('忘记密码')),
              )
            )
          ],
        ),
      ),
      )
    );
  }
}